//
//  AuctionCollectionViewCell.m
//  MyNewProject
//
//  Created by 王春辉 on 2019/12/22.
//  Copyright © 2019年 sun. All rights reserved.
//

#import "AuctionCollectionViewCell.h"

@interface AuctionCollectionViewCell() {
    UIColor *_normalColor; // 进行中的颜色
    UIColor *_endColor; // 活动结束颜色
}

/**
 距离结束时间label
 */
@property (nonatomic, strong) UILabel *endTimeLabel;

/**
 结束之后的label
 */
@property (nonatomic, strong) UILabel *endLabel;

/**
 titleLabel
 */
@property (nonatomic, strong) UILabel *titleLabel;

/**
 价格label
 */
@property (nonatomic, strong) UILabel *priceLabel;

/**
 价格title
 */
@property (nonatomic, strong) UILabel *priceTitleLabel;

/**
 分享按钮
 */
@property (nonatomic, strong) UIButton *shareButton;

@property (nonatomic, copy) ActionCollectionViewCellShareButtonClickBlock shareBlock;
@end
@implementation AuctionCollectionViewCell
@synthesize imageView = _imageView;

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.layer.masksToBounds = true;
        self.layer.cornerRadius = 4.0;
        self.backgroundColor = [UIColor whiteColor];
        _normalColor = SKColorWithHex(0xFF3030);
        _endColor = SKColorWithHex(0x888888);
        [self addObserver];
        [self initContentViews];
    }
    return self;
}
/**
 初始化contentViews
 */
- (void)initContentViews {
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_offset(0);
        make.height.mas_equalTo(self.imageView.mas_width);
    }];
    
    [self.endTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.imageView);
        make.height.mas_equalTo(self.endTimeLabel.mas_width).multipliedBy(25.0 / 170);
    }];
    
    [self.priceTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.bottom.mas_offset(-12);
        make.height.mas_equalTo(10);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.priceTitleLabel.mas_right);
        make.bottom.mas_equalTo(self.priceTitleLabel);
        make.height.mas_equalTo(13);
    }];
    
    [self.shareButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-10);
        make.bottom.mas_equalTo(self.priceTitleLabel);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.priceTitleLabel);
        make.right.mas_equalTo(self.shareButton);
        make.top.mas_equalTo(self.imageView.mas_bottom);
        make.bottom.mas_equalTo(self.priceLabel.mas_top);
    }];
    
    [self.endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.imageView);
    }];
    self.endLabel.hidden = true;
}

- (void)addObserver {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(countDownNoti:) name:OYCountDownNotification object:nil];
}
- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark -- mark 懒加载

- (UIImageView *)imageView {
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.backgroundColor = [UIColor lightGrayColor];
        [self addSubview:_imageView];
    }
    return _imageView;
}
- (UILabel *)endTimeLabel {
    if (!_endTimeLabel) {
        self.endTimeLabel = [[UILabel alloc] init];
        _endTimeLabel.backgroundColor = _normalColor;
        _endTimeLabel.layer.opacity = 0.8;
        _endTimeLabel.textAlignment = NSTextAlignmentCenter;
        _endTimeLabel.font = SKFONT_Medium(13);
        _endTimeLabel.textColor = [UIColor whiteColor];
        [self addSubview:_endTimeLabel];
    }
    return _endTimeLabel;
}
- (UILabel *)endLabel {
    if (!_endLabel) {
        self.endLabel = [[UILabel alloc] init];
        _endLabel.backgroundColor = _endColor;
        _endLabel.layer.opacity = 0.8;
        _endLabel.textColor = [UIColor whiteColor];
        _endLabel.textAlignment = NSTextAlignmentCenter;
        _endLabel.text = @"已结束";
        _endLabel.font = SKFONT_Medium(18);
        [self addSubview:_endLabel];
    }
    return _endLabel;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        self.titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = SKColorWithHex(0x333333);
        _titleLabel.font = SKFONT_BOLD_SIZE(13);
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        self.priceLabel = [[UILabel alloc] init];
        _priceLabel.textColor = _normalColor;
        _priceLabel.font = SKFONT_BOLD_SIZE(17);
        [self addSubview:_priceLabel];
    }
    return _priceLabel;
}
- (UILabel *)priceTitleLabel {
    if (!_priceTitleLabel) {
        self.priceTitleLabel = [[UILabel alloc] init];
        _priceTitleLabel.textColor = _normalColor;
        _priceTitleLabel.font = SKFONT_BOLD_SIZE(10);
        _priceTitleLabel.text = @"现价 ¥ ";
        [self addSubview:_priceTitleLabel];
    }
    return _priceTitleLabel;
}

- (UIButton *)shareButton {
    if (!_shareButton) {
        self.shareButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_shareButton setImage:SKIMAGE_NAMED(@"shop_share_btn_img") forState:UIControlStateNormal];
        [_shareButton addTarget:self action:@selector(shareButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        _shareButton.hidden = true;
        [self addSubview:_shareButton];
    }
    return _shareButton;
}

#pragma mark -- mark 传值

- (void)setTitle:(NSString *)title {
    _title = title;
    self.titleLabel.text = title;
}

- (void)setStartPrice:(CGFloat)startPrice {
    _startPrice = startPrice;
    self.priceLabel.text = [NSString stringWithFormat:@"%.2f", startPrice];
}

- (void)setStatus:(NSInteger)status {
    _status = status;
    switch (status) {
        case 1:
            //未开始
            [self readyStart];
            return;
        case 2:
            //进行中
            [self start];
            return;
        case 3:
            //已结束
            [self end];
            return;
        case 4:
            //已成交
            [self end];
            return;
        case 5:
            //流拍
            [self failed];
            return;
        default:
            break;
    }
}

/// 倒计时
/// @param noti nodti
- (void)countDownNoti:(NSNotification *)noti {
    if (self.status == 1 && self.sinceStartTimeInterval > 0) {
        // 距离开始
        self.sinceStartTimeInterval -= 1;
        if (self.sinceStartTimeInterval <= 0) {
            // 结束 刷新数据
            if (self.countDownEndBlock) {
                self.countDownEndBlock(); }
        }else {
            int hour = self.sinceStartTimeInterval / 3600;
            int minute = (int)(self.sinceStartTimeInterval / 60) % 60;
            int second = (int)self.sinceStartTimeInterval % 60;
            self.endTimeLabel.text = [NSString stringWithFormat:@"距开始 %.2d : %.2d : %.2d", hour, minute, second];
        }
    }else if (self.status == 2 && self.sinceEndTimeInterval > 0) {
        // 距离结束
        self.sinceEndTimeInterval -= 1;
        if (self.sinceEndTimeInterval <= 0) {
            // 结束 刷新数据
            if (self.countDownEndBlock) {
                self.countDownEndBlock(); }
        }else {
            int hour = self.sinceEndTimeInterval / 3600;
            int minute = (int)(self.sinceEndTimeInterval / 60) % 60;
            int second = (int)self.sinceEndTimeInterval % 60;
            self.endTimeLabel.text = [NSString stringWithFormat:@"距结束 %.2d : %.2d : %.2d", hour, minute, second];
        }
    }
}

#pragma mark -- mark EVENT

/**
 点击事件

 @param sender sender
 */
- (void)shareButtonClick:(UIButton *)sender {
    if (self.shareBlock) {
        self.shareBlock(); }
}


#pragma mark -- mark 业务逻辑

/**
 即将开始
 */
- (void)readyStart {
    self.endLabel.hidden = true;
    self.endTimeLabel.hidden = false;
    self.endTimeLabel.backgroundColor = _endColor;
    self.priceLabel.textColor = _endColor;
    self.priceTitleLabel.textColor = _endColor;
    self.priceTitleLabel.text = @"现价 ¥ ";
    self.endTimeLabel.text = [NSString stringWithFormat:@"距开始 %@", @"00 : 00 : 00"];
}

/**
 开始
 */
- (void)start {
    self.endLabel.hidden = true;
    self.endTimeLabel.hidden = false;
    self.endTimeLabel.backgroundColor = _normalColor;
    self.priceLabel.textColor = _normalColor;
    self.priceTitleLabel.textColor = _normalColor;
    self.priceTitleLabel.text = @"现价 ¥ ";
    self.endTimeLabel.text = [NSString stringWithFormat:@"距结束 %@", @"00 : 00 : 00"];
}

/**
 即将结束
 */
- (void)readyEnd {
    self.endLabel.hidden = true;
    self.endTimeLabel.hidden = false;
    self.endTimeLabel.backgroundColor = _normalColor;
    self.priceLabel.textColor = _normalColor;
    self.priceTitleLabel.textColor = _normalColor;
    self.priceTitleLabel.text = @"当前 ¥ ";
    self.endTimeLabel.text = [NSString stringWithFormat:@"距结束 %@", @"00 : 00 : 00"];
}

/**
 结束
 */
- (void)end {
    self.endLabel.hidden = false;
    self.endTimeLabel.hidden = true;
    self.priceLabel.textColor = _endColor;
    self.priceTitleLabel.textColor = _endColor;
    self.priceTitleLabel.text = @"结缘价 ¥ ";
}

/**
 流拍
 */
- (void)failed {
    self.endLabel.hidden = false;
    self.endTimeLabel.hidden = true;
    self.priceLabel.textColor = _endColor;
    self.priceTitleLabel.textColor = _endColor;
    self.priceTitleLabel.text = @"已结束";
}
/**
 分享按钮点击回调
 
 @param block block
 */
- (void)shareButtonClickBlock:(ActionCollectionViewCellShareButtonClickBlock)block {
    self.shareBlock = block;
}
@end
